package com.closer.server.api;

import com.closer.server.biz.user.dto.LoginRequest;
import com.closer.server.biz.user.dto.RegisterRequest;
import com.closer.server.biz.user.dto.SearchRequest;
import com.closer.server.biz.user.dto.UpdateRequest;
import com.closer.server.biz.user.dto.vo.UserVO;
import com.closer.server.support.common.BaseResponse;
import com.closer.server.support.common.ResultUtils;
import com.mybatisflex.core.paginate.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.closer.server.biz.user.service.UserService;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户 控制层。
 *
 * @author closer
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("/register")
    public BaseResponse<Long> register(@RequestBody RegisterRequest params){
        var userId = userService.register(params);
        return ResultUtils.success(userId);
    }

    @PostMapping("/login")
    public BaseResponse<UserVO> login(@RequestBody LoginRequest params){
        var userId = userService.login(params);
        return ResultUtils.success(userId);
    }

    @PostMapping("/logout")
    public BaseResponse<Boolean> logout(){
        var result = userService.logout();
        return ResultUtils.success(result);
    }

    @GetMapping("/login-info")
    public BaseResponse<UserVO> getLoginInfo(Long userId){
        var result = userService.getLoginInfo(userId);
        return ResultUtils.success(result);
    }

    @PostMapping("/update-login")
    public BaseResponse<Boolean> updateLoginInfo(@RequestBody UpdateRequest params){
        var result = userService.updateLoginInfo(params);
        return ResultUtils.success(result);
    }

    @PostMapping("/search")
    public BaseResponse<Page<UserVO>> search(@RequestBody SearchRequest params){
        var list = userService.search(params);
        return ResultUtils.success(list);
    }

}
